import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: Wy
 * @Time: 2019/11/27 18:21
 * @Description:
 * @Modified:
 */
public class MyTest02 {
    public static void main(String[] args) throws IOException {
        String path = "C:\\Users\\Dyson\\Desktop\\x.txt";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
        StringBuffer sb = new StringBuffer();
        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            sb.append(str);
        }
        bufferedReader.close();
        char[] chars = sb.toString().toCharArray();
        Map<Character, Integer> map = mapFunction(chars);
        Map<Character, Integer> sortedMap = sortByValue(map);
        int flag = 0;
        for (Map.Entry<Character, Integer> characterIntegerEntry : sortedMap.entrySet()) {
            if (flag == 10) {
                return;
            }
            System.out.println(characterIntegerEntry.getKey() + " : " + characterIntegerEntry.getValue());
            flag++;
        }

    }

    private static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
        Map<K, V> result = new LinkedHashMap<>();
        map.entrySet().stream()
                .sorted(Map.Entry.<K, V>comparingByValue()
                        .reversed()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        return result;
    }

    private static Map<Character, Integer> mapFunction(char[] chars) {
        Map<Character, Integer> map = new HashMap<>();
        if (!(chars == null || chars.length == 0)) {
            for (int i = 0; i < chars.length; i++) {
                if (null != map.get(chars[i])) {
                    map.put(chars[i], map.get(chars[i]) + 1);
                } else {
                    map.put(chars[i], 1);
                }
            }
        }

        return map;
    }
}
